﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.Account;
using Bricks.Site.Models;
using Bricks.Business.Service;
using Bricks.Business.Data.Repositories.SafetyManagement;
using Bricks.Business.Models.SafetyManagement;
using Bricks.Business.Data.Repositories.Account;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(ISafetyPlanService))]
    internal class SafetyPlanService : ISafetyPlanService
    {
        [Import]
        protected ISafetyPlanRepository SafetyPlanRepository { get; set; }

        [Import]
        protected IUserInfoRepository UserRepository { get; set; }
        [Import]
        protected IOrgInfoRepository OrgInfoRepository { get; set; }


        public IQueryable<SafetyPlan> SafetyPlans
        {
            get { return SafetyPlanRepository.Entities; }
        }

        public SafetyPlanView GetSafetyPlan(Guid id)
        {
            SafetyPlanView safetyPlanView = null;
            SafetyPlan record = SafetyPlans.FirstOrDefault(s => s.ID == id);
            if (record != null)
            {
                safetyPlanView = new SafetyPlanView();
                safetyPlanView.ID = record.ID;
                safetyPlanView.PlanName = record.PlanName;
                safetyPlanView.PublishTime = record.PublishTime;
                safetyPlanView.PublishOrgID = record.PublishOrgID;
                safetyPlanView.PublishOrgName = GetOrgInfoByID(record.PublishOrgID);
                safetyPlanView.PublishUserID = record.PublishUserID;
                safetyPlanView.PublishUserName = GetUserInfoByID(record.PublishUserID);
                safetyPlanView.AbstractDescription = record.AbstractDescription;
                safetyPlanView.Attachment = record.Attachment;
            }
            return safetyPlanView;
        }

        public string GetOrgInfoByID(Guid id)
        {
            string result = string.Empty;
            var org = OrgInfoRepository.Entities.FirstOrDefault(m => m.ID == id);
            if (org != null)
            {
                result = org.OrgName;
            }
            return result;
        }

        public string GetUserInfoByID(Guid id)
        {
            string result = string.Empty;
            var user = UserRepository.Entities.FirstOrDefault(m => m.ID == id);
            if (user != null)
            {
                result = user.UserName;
            }
            return result;
        }

        public OperationResult SaveSafetyPlan(SafetyPlanView safetyPlanView)
        {
            string message = "操作成功！";
            try
            {
                SafetyPlan record = SafetyPlans.FirstOrDefault(s => s.ID == safetyPlanView.ID);
                if (record != null)
                {
                    Modify(record, safetyPlanView);
                }
                else
                {
                    Add(safetyPlanView);
                }
                return new OperationResult(OperationResultType.Success, "操作成功");
            }
            catch (Exception ex)
            {
                return new OperationResult(OperationResultType.Error, ex.Message);
            }

        }

        public void Add(SafetyPlanView safetyPlanView)
        {
            SafetyPlan record = new SafetyPlan();
            record.ID = safetyPlanView.ID;
            record.PlanName = safetyPlanView.PlanName;
            record.PublishTime = safetyPlanView.PublishTime;
            record.PublishOrgID = safetyPlanView.PublishOrgID;
            record.PublishUserID = safetyPlanView.PublishUserID;
            record.AbstractDescription = safetyPlanView.AbstractDescription;
            record.Attachment = safetyPlanView.Attachment;
            SafetyPlanRepository.Insert(record);
        }

        private void Modify(SafetyPlan record, SafetyPlanView safetyPlanView)
        {
            record.PlanName = safetyPlanView.PlanName;
            record.PublishTime = safetyPlanView.PublishTime;
            record.PublishOrgID = safetyPlanView.PublishOrgID;
            record.PublishUserID = safetyPlanView.PublishUserID;
            record.AbstractDescription = safetyPlanView.AbstractDescription;
            record.Attachment = safetyPlanView.Attachment;
            SafetyPlanRepository.Insert(record);
        }

        public OperationResult Delete(Guid id)
        {
            PublicHelper.CheckArgument(id, "id");
            SafetyPlan record = SafetyPlans.FirstOrDefault(s => s.ID == id);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            if (SafetyPlanRepository.Delete(record) >= 0)
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            return new OperationResult(OperationResultType.Error, "删除失败");
        }

    }
}